package com.jpcc.vw.onlineRadio.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.RemoteException;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.adapter.item.AlbumDeailItem;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.action.DetailItemClickAction;
import com.jpcc.vw.onlineRadio.model.action.DownloadTrackAction;
import com.jpcc.vw.onlineRadio.model.bean.RecentAlbumRadioTrackBean;
import com.jpcc.vw.onlineRadio.model.bean.Track;
import com.jpcc.vw.onlineRadio.recycle.RecyclerView.BaseItem;
import com.jpcc.vw.onlineRadio.recycle.RecyclerView.MultiTypeListAdapter;
import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.jpcc.vw.onlineRadio.utils.TrackUtils;
import com.jpcc.vw.onlineRadio.widget.Indicator;
import com.lzy.okhttpserver.download.DownloadInfo;
import com.lzy.okhttpserver.download.DownloadManager;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class AlbumDetailAdapter extends BaseRecyclerAdapter<Track, AlbumDetailAdapter.ViewHolder> {


    private List<Track> datas = new ArrayList<>();
    private Context context;
    private boolean ShowPayMark;
//    private Long albumId;

    public AlbumDetailAdapter(Context c, List<Track> datasLocal, boolean show) {
        super(c, datasLocal);
        context = c;
        this.datas.clear();
        this.datas.addAll(datasLocal);
        this.ShowPayMark = show;
    }

    public void setData(List<Track> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_album_detail_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Track model = datas.get(position);
        holder.bind(model);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private Track result;
        @BindView(R.id.tv_item_count)
        TextView tv_item_count;
        @BindView(R.id.tv_content2)
        TextView tv_content2;
        @BindView(R.id.imv_download)
        ImageView imv_download;
        @BindView(R.id.layout_top)
        RelativeLayout layout_top;
//        @BindView(R.id.imv_playing)
//        Indicator imv_playing;
        //        private AnimationDrawable frameAnim;
        @BindView(R.id.imv_nonFree)
        ImageView imv_nonFree;
        @BindView(R.id.imv_play_status)
        ImageView imv_play_status;

        private DownloadManager downloadManager;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(Track track) {
            this.result = track;


            tv_content2.setText(result.getTrackTitle());


            if (ShowPayMark) {
                imv_nonFree.setVisibility(View.GONE);
            } else {
                if (!result.isIs_paid()) {
                    imv_nonFree.setVisibility(View.GONE);
                } else {
                    if (result.isIs_free()) {
                        imv_nonFree.setVisibility(View.GONE);
                    } else {
                        imv_nonFree.setVisibility(View.VISIBLE);
                    }
                }
            }


            if ((!TextUtils.isEmpty(result.getDownloadUrl())) && (result.getDownloadUrl().length() > 5)) {
//                String path_local = TrackUtils.getTrackDir() + result.getSubordinatedAlbum().getId() + result.getId() + "." + result.getDownloadUrl().substring(result.getDownloadUrl().length() - 3, result.getDownloadUrl().length());
//                File file_local = new File(path_local);
                if (downloadManager == null) {
                    downloadManager = DownloadManager.getInstance(context);
                }

                DownloadInfo downloadInfo = downloadManager.getTaskByUrl(result.getDownloadUrl());
                if (downloadInfo != null) {

                    Logger.i("downloadinfoUrl-->" + result.getDownloadUrl());
                    Logger.i("downloadinfoStatus-->" + downloadInfo.getState());


                    if (downloadInfo.getState() == DownloadManager.NONE) {
//                        imv_download.setImageResource(R.drawable.detail_not_download);
                        Glide.with(context).load(R.drawable.detail_not_download).asBitmap().into(imv_download);
                        imv_download.setVisibility(View.VISIBLE);
                    } else if (downloadInfo.getState() == DownloadManager.WAITING || downloadInfo.getState() == DownloadManager.DOWNLOADING) {
//                        imv_download.setImageResource(R.drawable.detail_loading);
                        Glide.with(context).load(R.drawable.detail_loading).asBitmap().into(imv_download);
                        imv_download.setVisibility(View.VISIBLE);
                    } else if (downloadInfo.getState() == DownloadManager.FINISH) {
//                        imv_download.setImageResource(R.drawable.detail_success);
                        Glide.with(context).load(R.drawable.detail_success).asBitmap().into(imv_download);
                        imv_download.setVisibility(View.VISIBLE);
                    } else if (downloadInfo.getState() == DownloadManager.ERROR) {
                        Glide.with(context).load(R.drawable.detail_not_download).asBitmap().into(imv_download);
                        imv_download.setVisibility(View.VISIBLE);
                        downloadManager.removeTaskByTrackId(result.getId() + "");
                    }
                } else {
                    Glide.with(context).load(R.drawable.detail_not_download).asBitmap().into(imv_download);
                    imv_download.setVisibility(View.VISIBLE);
                }
            } else {
                Glide.with(context).load(R.drawable.cannotdownload).asBitmap().into(imv_download);
                imv_download.setVisibility(View.VISIBLE);
            }
            itemView.setOnClickListener(this);

            String bgColor = "#404040";
            if (Constants.isRelease) {
                try {
                    boolean atmosphereStatus = AtmosphereLampManager.getInstance(context).isHmiAtmosphereLampOn();
                    String color = AtmosphereLampManager.getInstance(context).getHmiAtmosphereLampColor();
                    if (atmosphereStatus) {
                        if (!TextUtils.isEmpty(color)) {
                            bgColor = color;
                        }
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            if (AlbumRadioAudioPlayer.get().getPlayMusic() != null) {
                RecentAlbumRadioTrackBean lastAlbum = AlbumRadioAudioPlayer.get().getPlayMusic();
                if (result.getId() == lastAlbum.getTrackId()) {
//                    layout_top.setBackgroundColor(Color.parseColor(bgColor));
                    layout_top.setBackgroundColor(Color.parseColor("#000000"));
//                    imv_playing.setVisibility(View.VISIBLE);
                    imv_play_status.setVisibility(View.VISIBLE);
                } else {
                    layout_top.setBackgroundColor(Color.parseColor("#000000"));
//                    imv_playing.setVisibility(View.GONE);
                    imv_play_status.setVisibility(View.GONE);
                }
            } else {
                layout_top.setBackgroundColor(Color.parseColor("#000000"));
//                imv_playing.setVisibility(View.GONE);
                imv_play_status.setVisibility(View.GONE);
            }

            if (AlbumRadioAudioPlayer.get().isPlaying() || AlbumRadioAudioPlayer.get().isPreparing()) {
//                imv_playing.setIndicatorPause(true);
                imv_play_status.setImageResource(R.drawable.ic_play_in_list);
            } else {
//                imv_playing.setIndicatorPause(false);
                imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
            }
        }


        @OnClick(R.id.imv_download)
        public void download() {
            if (result.isCanDownload() && !TextUtils.isEmpty(result.getDownloadUrl())) {
                String path = TrackUtils.getTrackDir() + result.getSubordinatedAlbum().getId() + result.getId() + "." + result.getDownloadUrl().substring(result.getDownloadUrl().length() - 3, result.getDownloadUrl().length());
                File file = new File(path);
                Logger.i("path-->" + path);
                DownloadTrackAction action = new DownloadTrackAction();
                action.setAlbumId(result.getSubordinatedAlbum().getId());
                action.setTrackId(result.getId());
                action.setTrack(result);
                action.setDir(TrackUtils.getTrackDir());
                action.setFileName(result.getSubordinatedAlbum().getId() + "" + result.getId() + "." + result.getDownloadUrl().substring(result.getDownloadUrl().length() - 3));
                EventBus.getDefault().post(action);
            } else {
//                ToastUtils.show(R.string.string_95);
            }
        }

        @Override
        public void onClick(View v) {
            DetailItemClickAction action = new DetailItemClickAction();
            action.setPosition(getPosition());
            EventBus.getDefault().post(action);
//            imv_playing.setIndicatorPause(true);
            imv_play_status.setImageResource(R.drawable.ic_play_in_list);
        }
    }
}
